package com.example.text.ui.mall.adepter


import android.view.View
import android.widget.TextView
import androidx.databinding.ViewDataBinding

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.text.R
import com.example.text.databinding.ItemMallEvaluation0v0pBinding
import com.example.text.databinding.ItemMallEvaluation0v3pBinding
import com.example.text.databinding.ItemMallEvaluation1v0pBinding
import com.example.text.databinding.ItemMallEvaluation1v1pBinding
import com.example.text.databinding.ItemMallEvaluation1v2pBinding
import com.example.text.ui.mall.model.Info
import com.example.text.ui.mall.model.MallEvaluation

//import com.jczy.cyclone.R
//import com.jczy.cyclone.databinding.*
//import com.jczy.cyclone.model.MallEvaluation
//import com.jczy.cyclone.ui.player.VideoPlayerActivity

/**
 * Created by ZengCS on 2023/3/9.
 * E-mail:zengcs@vip.qq.com
 * Add:中国成都
 * Desc: 商品评价
 */
class EvaluationListMultiAdapter(
    data: ArrayList<MallEvaluation>,
    private val onImageClick: ((Int, Int, List<String>, List<Info>?) -> Unit)? = null,
    private val onReportClick: ((item: MallEvaluation) -> Unit)? = null
) : BaseMultiItemQuickAdapter<MallEvaluation, BaseDataBindingHolder<ViewDataBinding>>(data),
    LoadMoreModule {
    companion object {
        const val TYPE_0V0P = 0
        const val TYPE_0V9P = 1
        const val TYPE_1V0P = 2
        const val TYPE_1V1P = 3
        const val TYPE_1V2P = 4

        // 商品详情-评论列表
        const val TYPE_0V4P = 5

        // 售后详情
        const val TYPE_AFTER_DETAIL = 999

        const val REPLAY_MAX_LINE = 2
    }

    init {
        addItemType(TYPE_0V0P, R.layout.item_mall_evaluation_0v0p)
        addItemType(TYPE_0V9P, R.layout.item_mall_evaluation_0v3p)
        addItemType(TYPE_1V0P, R.layout.item_mall_evaluation_1v0p)
        addItemType(TYPE_1V1P, R.layout.item_mall_evaluation_1v1p)
        addItemType(TYPE_1V2P, R.layout.item_mall_evaluation_1v2p)
    }

    private fun convertMoreReplay(tvReplay: TextView, tvMoreReplay: TextView, item: MallEvaluation) {
        /** 处理商家回复 */
        tvReplay.post {
            tvMoreReplay.visibility = View.GONE
            val lineCount = tvReplay.lineCount
            if (lineCount < 1 || item.hasShowMore)
                return@post
            val ellipsisCount = tvReplay.layout.getEllipsisCount(lineCount - 1)
            val isEllipsized = ellipsisCount > 0
            if (lineCount > REPLAY_MAX_LINE || isEllipsized) {
                tvMoreReplay.visibility = View.VISIBLE

                val originalText = tvReplay.text.toString()
                // 计算最后一行的字符数
                val endOffset = tvReplay.layout.getLineEnd(REPLAY_MAX_LINE - 1)
                val trimmedText = originalText.substring(0, endOffset - 3) + "..."
                tvReplay.text = trimmedText

                tvMoreReplay.setOnClickListener {
                    item.hasShowMore = true
                    tvMoreReplay.visibility = View.GONE
                    tvReplay.text = originalText
                }
            }
        }
    }

    override fun convert(holder: BaseDataBindingHolder<ViewDataBinding>, item: MallEvaluation) {
        when (item.itemType) {
            TYPE_0V0P -> {
                val binding = holder.dataBinding as ItemMallEvaluation0v0pBinding
                binding.item = item
                binding.iconMoreDot.setOnClickListener {
                    onReportClick?.invoke(item)
                }

                /** 处理商家回复 */
                convertMoreReplay(binding.tvReplay, binding.tvMoreReplay, item)
            }
            TYPE_0V9P -> {
                val binding = holder.dataBinding as ItemMallEvaluation0v3pBinding
                binding.item = item
                binding.iconMoreDot.setOnClickListener {
                    onReportClick?.invoke(item)
                }
//                // 展示图片
//                binding.nineGridView.setPicsList(item.itemType, item.transImgs() ?: emptyList())
//                onImageClick?.let {
//                    binding.nineGridView.setOnItemClickListener(onImageClick)
//                }
//                binding.nineGridView.initView(holder.absoluteAdapterPosition)

                /** 处理商家回复 */
                convertMoreReplay(binding.tvReplay, binding.tvMoreReplay, item)
            }
            TYPE_1V0P -> {
                val binding = holder.dataBinding as ItemMallEvaluation1v0pBinding
                binding.item = item
                binding.iconMoreDot.setOnClickListener {
                    onReportClick?.invoke(item)
                }
                /** 视频播放相关 */
                binding.ivVideoCover.setOnClickListener {
//                    VideoPlayerActivity.jump(videoUrl = item.videoUrl ?: "")
                }
                /** 处理商家回复 */
                convertMoreReplay(binding.tvReplay, binding.tvMoreReplay, item)
            }
            TYPE_1V1P -> {
                val binding = holder.dataBinding as ItemMallEvaluation1v1pBinding
                binding.item = item
                binding.iconMoreDot.setOnClickListener {
                    onReportClick?.invoke(item)
                }
                // 展示图片
//                binding.nineGridView.setPicsList(item.itemType, item.transImgs() ?: emptyList())
//                onImageClick?.let {
//                    binding.nineGridView.setOnItemClickListener(onImageClick)
//                }
//                binding.nineGridView.initView(holder.absoluteAdapterPosition)
//                /** 视频播放相关 */
//                binding.ivVideoCover.setOnClickListener {
//                    VideoPlayerActivity.jump(videoUrl = item.videoUrl ?: "")
//                }
                /** 处理商家回复 */
                convertMoreReplay(binding.tvReplay, binding.tvMoreReplay, item)
            }
            TYPE_1V2P -> {
                val binding = holder.dataBinding as ItemMallEvaluation1v2pBinding
                binding.item = item
                binding.iconMoreDot.setOnClickListener {
                    onReportClick?.invoke(item)
                }
                // 展示图片
//                binding.nineGridView.setPicsList(item.itemType, item.transImgs(2) ?: emptyList(), item.transImgs())
//                onImageClick?.let {
//                    binding.nineGridView.setOnItemClickListener(onImageClick)
//                }
//                binding.nineGridView.initView(holder.absoluteAdapterPosition)
//                /** 视频播放相关 */
//                binding.ivVideoCover.setOnClickListener {
//                    VideoPlayerActivity.jump(videoUrl = item.videoUrl ?: "")
//                }
                /** 处理商家回复 */
                convertMoreReplay(binding.tvReplay, binding.tvMoreReplay, item)
            }
        }
        holder.dataBinding?.executePendingBindings()
    }
}